package cn.com.yang.egg.memoryDemo;

public class Test {
	
		private static final int SIZE=10000;
		
		public static void main(String[] args) {
			LotsOfBooleans[] first = new LotsOfBooleans[SIZE];
			LostOfInts[] second = new LostOfInts[SIZE];
			System.gc();
			long startMem=getMemory();
			for (int i = 0; i < SIZE; i++) {
				first[i]=new LotsOfBooleans();
			}
			//
			System.gc();
			long endMem=getMemory();
			System.out.println("Size for LotsOfBooleans:"+(endMem-startMem));
			System.out.println("Average size:"+(endMem-startMem)/(double)SIZE);
			//
			System.gc();
			startMem=getMemory();
			for (int i = 0; i < SIZE; i++) {
				second[i] = new LostOfInts();
			}
			//
			System.gc();
			endMem=getMemory();
			System.out.println("Size for LotsOfInts:"+(endMem-startMem));
			System.out.println("Average size:"+(endMem-startMem)/(double)SIZE);
			long total=0;
			for (int i = 0; i < SIZE; i++) {
				total+=(first[i].a0 ? 1:0) + second[i].a0;
			}
			System.out.println("total:"+total);
			
			/**
			 *  Size for LotsOfBooleans:120144
			 *  Average size:12.0144    //12
				Size for LotsOfInts:3359984
				Average size:335.9984   //336
				total:0
			 */
		}

		private static long getMemory() {
			Runtime runtime = Runtime.getRuntime();
			return runtime.totalMemory() - runtime.freeMemory();
		}

}
